<?php
include dirname(dirname(dirname(__FILE__))).'/include/user-verify.php';
include dirname(dirname(__FILE__)).'/user-manage/functions.php';
include_once dirname(dirname(dirname(__FILE__))).'/include/host-base.php';

$id = addslashes($_SESSION['user_id']);
$row = getUserById($id);


?>

<!DOCTYPE html>
<html>
	<head>
		<?php include dirname(dirname(dirname(__FILE__))).'/include/common-header.php'; ?>


		<?php 
			if(isset($_POST['submited'])) {
				$old_password = md5(addslashes($_POST['old-password']));
				$new_password = md5(addslashes($_POST['new-password']));
				if($old_password!=$row['password']) {
					echo '<script>alert("当前密码不正确");
						 window.location="'.getHost().'/admin/personal-info-manage/password-edit.php";</script>';
					exit();
				} else {
					$sql = 'update user set password = "'.$new_password.'" where id = '.$id.';';
					$conn = get_db_conn();
					mysql_query($sql,$conn) or die ("数据库操作失败");
					echo '<script>alert("修改密码成功，请记住你的新密码");
						 window.location="'.getHost().'/admin/personal-info-manage/index.php";</script>';
				}
			}



		?>

		<script type="text/javascript">
		$(document).ready(function() {
			$("input").attr("maxlength","16");
			$("#new-password").blur(function() {
				var _password = $.trim($(this).val());
				if(_password.length<4) {
					$("#new-password").parent().parent().addClass("has-error");
					$("#new-password").next().html("密码长度应为4~16位").fadeIn();
					$("#password-edit-btn").attr("disabled","disabled");
				} else {
					$("#new-password").parent().parent().removeClass("has-error");
					$("#new-password").next().hide();
					$("#password-edit-btn").removeAttr("disabled");
				}
			});
			$("#confirm-password").blur(function() {
				var _password = $.trim($(this).val());
				if(_password!=$.trim($("#new-password").val())) {
					$("#confirm-password").parent().parent().addClass("has-error");
					$("#confirm-password").next().html("两次输入密码不一致").fadeIn();
					$("#password-edit-btn").attr("disabled","disabled");
				}  else {
					$("#confirm-password").parent().parent().removeClass("has-error");
					$("#confirm-password").next().hide();
					if($.trim($("#new-password").val()).length>=4)$("#password-edit-btn").removeAttr("disabled");
				}
			});
			$("#password-edit-btn").click(function() {
				if($.trim($("#new-password").val()).length<4) {
					$("#new-password").parent().parent().addClass("has-error");
					$("#new-password").next().html("密码长度应为4~16位").fadeIn();
					$("#password-edit-btn").attr("disabled","disabled");
					return false;
				} else if($.trim($("#new-password").val())!=$.trim($("#confirm-password").val())) {
					$("#confirm-password").parent().parent().addClass("has-error");
					$("#confirm-password").next().html("两次输入密码不一致").fadeIn();
					$("#password-edit-btn").attr("disabled","disabled");
					return false;
				}
			});
		}); 
		</script>

		<link rel="stylesheet" type="text/css" href="../css/style.css">
	</head>
	<body>
		<div class="col-md-12">
			<div>
				<h3>修改登陆密码</h3>
			</div>
			<form class="form-horizontal" role="form" method="post" action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>>
			  
				<div class="form-group">
			    <label for="old-password" class="col-sm-2 control-label">输入当前密码</label>
			    <div class="col-sm-10">
			      <input type="password" name="old-password" class="form-control normal-input" 
			      value="" id="old-password" placeholder="输入当前密码">
			      <p class="help-block" style="display:none"></p>
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="new-password" class="col-sm-2 control-label">新的密码</label>
			    <div class="col-sm-10">
			      <input type="password" name="new-password" class="form-control normal-input" 
			      value="" id="new-password" placeholder="新的密码">
			      <p class="help-block" style="display:none"></p>
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="confirm-password" class="col-sm-2 control-label">密码确认</label>
			    <div class="col-sm-10">
			      <input type="password" name="confirm-password" class="form-control normal-input" 
			      value="" id="confirm-password" placeholder="请再次输入密码">
			      <p class="help-block" style="display:none"></p>
			    </div>
			  </div>
			  

			  <div class="form-group">
			    <div class="col-sm-offset-2 col-sm-10">
			      <button type="submit" class="btn btn-primary" id="password-edit-btn" name="submited">确认修改</button>
			    </div>
			  </div>
			</form>			
		</div>
	</body>
</html>